home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / demo / pwrtcp11.exe / ECHOC.C_ / ECHOC.bin
Text File  |  1994-11-11  |  10KB  |  353 lines

  1. //************************************************************************
  2. //
  3. //  Module: echoc.c
  4. //
  5. //  Purpose:
  6. //    ECHOC.EXE demonstrates the PowerTCP C DLL Interface for TCP
  7. //    This has been compiled in 16 and 32 bit versions
  8. //    This project accomplished on 30 Jul 94
  9. //    Listens on port 7 for a connection and echos back all data received
  10. //    If a second connection occurs, the first is terminated
  11. //             
  12. //************************************************************************
  13. //
  14. //  Written by Dart Communication Application Programming Group.
  15. //  Copyright (c) 1994 Dart Communications.  All Rights Reserved.
  16. //
  17. //************************************************************************
  18.  
  19. #define STRICT       // be bold!
  20. #pragma warning (disable:4100 4355 4699)
  21.  
  22. #include <windows.h>
  23. #include <string.h>
  24.  
  25. #include "..\..\include\powertcp.h"
  26. #include "echo.h"
  27. #include "echoc.hh"
  28.  
  29. HPOWERTCP     hListener = NULL; // session identifier for listener
  30. HPOWERTCP    hSession = NULL; // session identifier for echo-er
  31. HINSTANCE    hInstance = NULL;
  32. HWND            hWnd; // handle of main window 
  33. char *      Class="PowerEcho";
  34. HICON            hIcon;
  35.  
  36. int PASCAL WinMain( HINSTANCE hNewInstance, HINSTANCE hPrevInstance,
  37.                     LPSTR lpszCmdLine, int nCmdShow )
  38. {
  39.    MSG   msg ;
  40.  
  41.    hInstance=hNewInstance;  // init for later
  42.  
  43.    if (!hPrevInstance)
  44.       if (!InitApplication())
  45.          return ( FALSE ) ;
  46.  
  47.    if (!InitInstance( nCmdShow ))
  48.       return ( FALSE ) ;
  49.  
  50.    while (GetMessage( &msg, NULL, 0, 0 ))
  51.    {
  52.       {
  53.          TranslateMessage( &msg ) ;
  54.          DispatchMessage( &msg ) ;
  55.       }
  56.    }
  57.    
  58.    if (UnregisterClass(Class,hInstance) && hIcon)
  59.        DestroyIcon(hIcon);
  60.    
  61.    return ( (int) msg.wParam ) ;
  62.  
  63. } // end of WinMain()
  64.  
  65. BOOL InitApplication(void)
  66. {
  67.    WNDCLASS  wndclass ;
  68.  
  69.    // register window class
  70.  
  71.    wndclass.style =         0 ;
  72.    wndclass.lpfnWndProc =   WndProc ;
  73.    wndclass.cbClsExtra =    0 ;
  74.    wndclass.cbWndExtra =    0;
  75.    wndclass.hInstance =     hInstance ;
  76.    hIcon=wndclass.hIcon =         LoadIcon( hInstance, "PowerEcho" );
  77.    wndclass.hCursor =       LoadCursor( NULL, IDC_ARROW ) ;
  78.    wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  79.    wndclass.lpszMenuName =  MAKEINTRESOURCE( TTYMENU ) ;
  80.    wndclass.lpszClassName = Class ;
  81.  
  82.    return( RegisterClass( &wndclass ) ) ;
  83.  
  84. } // end of InitApplication()
  85.  
  86. void Display (LPCSTR Message)
  87.     {
  88.     // put Message into display box
  89.     HWND hEditWnd=GetDlgItem(hWnd,EDITWND);
  90.     if (IsWindow(hEditWnd))
  91.         {
  92.         //SendMessage(hEditWnd,EM_SETSEL,0,MAKELONG(32000,32000));
  93.         SendMessage(hEditWnd,EM_REPLACESEL,0,(LPARAM)Message);
  94.         SendMessage(hEditWnd,EM_REPLACESEL,0,(LPARAM)(LPSTR)"\r\n");
  95.         //SendMessage(hEditWnd,EM_SETSEL,0,MAKELONG(0,0));
  96.         }
  97.     }
  98.  
  99.  
  100.  
  101. // all PowerTCP callback functions go here so we don't have to prototype
  102. // them...
  103.  
  104. void CALLBACK ConnectEvent(
  105.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  106.     DWORD     UserData,  /* UserData from Connect() function */
  107.     LPCSTR    RemoteDotAddr, /* remote host connected to in dot notation */
  108.     WORD      RemotePort,/* remote port connected to */
  109.     LPCSTR    LocalDotAddr, /* local host address in dot notation */
  110.     WORD      LocalPort, /* local port */
  111.     LPCSTR    LocalName  /* name of default local host */
  112.     )
  113. {
  114.     char Buf[100]; 
  115.     unsigned int Port=(unsigned int)RemotePort;
  116.     // if connected to someone else, kill it
  117.     if (hSession)
  118.         {
  119.         Display("Closing Echo Connect to accept a new one...");
  120.         CloseTcp(hSession,TRUE); // abort 
  121.         }
  122.         
  123.     hSession=hPowerTcp;
  124.     Display("Connection information:");
  125.     wsprintf(Buf,"  Remote host %s on remote port %u connected to %s",
  126.         RemoteDotAddr, Port, LocalName);
  127.     Display(Buf);
  128.     Port=(unsigned int)LocalPort;
  129.     wsprintf(Buf,"    on our host %s local port %u",LocalDotAddr,Port);
  130.     Display(Buf);
  131. }
  132.     
  133. void CALLBACK RecvEvent(
  134.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  135.     DWORD     UserData,  /* UserData from Open() function */
  136.     LPBYTE    Data,  /* data from remote source */ 
  137.     size_t    Cnt        /* byte count for RecvData */
  138.     )
  139. {
  140.     if (!Data)
  141.         if (hPowerTcp==hListener)
  142.             Display("Listener has closed");
  143.         else
  144.             Display("Echo session has closed");
  145.     else
  146.         // echo data
  147.         SendTcp(hSession,Data,Cnt,FALSE,0);
  148. }    
  149.  
  150. void CALLBACK SendEvent(
  151.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  152.     DWORD     UserData,  /* UserData from Open() function */
  153.     DWORD     SendInstance /* SendInstance from Send() functions */
  154.     )
  155. {
  156.     Display ("Echo Sent Successfully!");
  157. }
  158.  
  159. void CALLBACK ListenEvent(
  160.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  161.     DWORD     UserData,  /* UserData from Connect() function */
  162.     LPCSTR    LocalDotAddr, /* local host address in dot notation */
  163.     WORD      LocalPort, /* local port */
  164.     LPCSTR    LocalName  /* name of default local host */
  165.     )
  166. {
  167.     // called, so we are listening...
  168.     char Buf[100];
  169.     unsigned int Port=(unsigned int)LocalPort; 
  170.     hListener=hPowerTcp;
  171.     wsprintf(Buf,"Listening on %s port %u (%s)",LocalDotAddr,Port,LocalName);
  172.     Display(Buf);
  173. }
  174.  
  175. void CALLBACK ExceptionEvent(
  176.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  177.     DWORD     UserData,  /* UserData from Open() function */
  178.     PT_EXCEPTION ErrorCode,  /* spontaneous error notification */
  179.     LPCSTR    ErrorDesc  /* description of error */
  180.     )          
  181. {  
  182.     Display(ErrorDesc);
  183. }
  184.     
  185. void CALLBACK AcceptEvent(
  186.     HPOWERTCP hPowerTcp,    /* identifies session notifying our app */ 
  187.     DWORD     UserData  /* UserData from Listen() function */
  188.     )
  189. {
  190.     // must create a new session to handle the connection 
  191.     AcceptTcp(
  192.         0ul,      /* user-defined data passed back by all callback functions */ 
  193.         NULL,    /* use license number assigned to you by Dart */
  194.         PT_SHOW,                 
  195.         hPowerTcp,    /* hPowerTcp that generated AcceptEvent() */ 
  196.         ConnectEvent, /* your event handler for session Open/Close notification */ 
  197.         RecvEvent, /* your event handler for receiving data */ 
  198.         SendEvent, /* your event handler for Send confirmation */ 
  199.         ExceptionEvent); /* your event handler for exception notifications */
  200. }
  201.                        
  202. // main window procedure
  203.  
  204. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg,
  205.                                WPARAM wParam, LPARAM lParam )
  206. {
  207.    switch (uMsg)
  208.    {
  209.       case WM_CREATE:
  210.              {
  211.             // set Gcp220
  212.             // create an edit window
  213.             HWND hEdit=CreateWindow("edit",
  214.                 NULL, // no window name
  215.                 WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|ES_READONLY,
  216.                 0,0,0,0,
  217.                 hWnd,
  218.                 (HMENU)EDITWND, // my child ID
  219.                 hInstance,
  220.                 NULL);
  221.  
  222.             break;
  223.             }
  224.  
  225.       case WM_COMMAND:
  226.       {
  227.          switch ((WORD) wParam)
  228.          {
  229.              case IDM_ABOUT:
  230.                  DialogBoxParam( hInstance, MAKEINTRESOURCE( ABOUTDLGBOX ), hWnd, AboutDlgProc, 0l ) ;
  231.                break;
  232.  
  233.             case IDM_EXIT:
  234.                PostMessage( hWnd, WM_CLOSE, 0, 0L ) ;
  235.                break ;
  236.          }
  237.       }
  238.       break ;
  239.  
  240.       case WM_SIZE:
  241.             MoveWindow(GetDlgItem(hWnd,EDITWND),0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
  242.          break ;
  243.  
  244.       case WM_DESTROY:
  245.          PostQuitMessage( 0 ) ;
  246.          break ;
  247.  
  248.       case WM_CLOSE:
  249.           if (hSession)
  250.               CloseTcp(hSession,TRUE); // abort
  251.           if (hListener)
  252.               CloseTcp(hListener,TRUE); // abort
  253.          DialogBoxParam( hInstance, MAKEINTRESOURCE( ABOUTDLGBOX ), hWnd, AboutDlgProc, 0l ) ;
  254.          // fall through
  255.  
  256.       default:
  257.          return( DefWindowProc( hWnd, uMsg, wParam, lParam ) ) ;
  258.    }
  259.    return 0L ;
  260.  
  261. } // end of WndProc()
  262.  
  263. BOOL InitInstance( int nCmdShow )
  264. {
  265.     // listen for duration of program
  266.    //FARPROC test=MakeProcInstance((FARPROC)ListenEvent,hInstance);
  267.  
  268.    // create the TTY window
  269.    hWnd = CreateWindow( Class, "PowerTCP ECHO Demo",
  270.                            WS_OVERLAPPEDWINDOW,
  271.                            CW_USEDEFAULT, CW_USEDEFAULT,
  272.                            CW_USEDEFAULT, CW_USEDEFAULT,
  273.                            NULL, NULL, hInstance, NULL ) ;
  274.  
  275.    if (!hWnd)
  276.       return ( FALSE ) ;
  277.       
  278.                 
  279.     ListenTcp(
  280.         0ul, // no user-data saved
  281.         NULL, // contact Dart for your LicenseKey
  282.         PT_SHOW,
  283.         NULL, // local address is default local host
  284.         7, // listen on the well-known echo port)
  285.         ListenEvent, // our callback procedure
  286.         RecvEvent,
  287.         AcceptEvent, // our callback for accepted connections
  288.         ExceptionEvent); // for exceptions
  289.    
  290.  
  291.    ShowWindow( hWnd, nCmdShow ) ;
  292.    UpdateWindow( hWnd ) ;
  293.    
  294.    return ( TRUE ) ;
  295.  
  296. } // end of InitInstance()
  297.  
  298.  
  299. BOOL CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg,
  300.                               WPARAM wParam, LPARAM lParam )
  301. {
  302.    switch (uMsg)
  303.    {
  304.       case WM_INITDIALOG:
  305.       {
  306.  
  307. #ifdef ABOUTDLG_USEBITMAP
  308.          // if we are using the bitmap, hide the icon
  309.  
  310.          ShowWindow( GetDlgItem( hDlg, IDD_ABOUTICON ), SW_HIDE ) ;
  311. #endif
  312.  
  313.       }
  314.       return ( TRUE ) ;
  315.  
  316. #ifdef ABOUTDLG_USEBITMAP
  317.       // used to paint the bitmap
  318.  
  319.       case WM_PAINT:
  320.       {
  321.          HBITMAP      hBitMap ;
  322.          HDC          hDC, hMemDC ;
  323.          PAINTSTRUCT  ps ;
  324.  
  325.          // load bitmap and display it
  326.  
  327.          hDC = BeginPaint( hDlg, &ps ) ;
  328.          if (NULL != (hMemDC = CreateCompatibleDC( hDC )))
  329.          {
  330.             hBitMap = LoadBitmap( hInstance,
  331.                                   MAKEINTRESOURCE( TTYBITMAP ) ) ;
  332.             hBitMap = (HBITMAP)SelectObject( hMemDC, hBitMap ) ;
  333.             BitBlt( hDC, 10, 10, 64, 64, hMemDC, 0, 0, SRCCOPY ) ;
  334.             DeleteObject( SelectObject( hMemDC, hBitMap ) ) ;
  335.             DeleteDC( hMemDC ) ;
  336.          }
  337.          EndPaint( hDlg, &ps ) ;
  338.       }
  339.       break ;
  340. #endif
  341.  
  342.       case WM_COMMAND:
  343.          if ((WORD) wParam == IDD_OK)
  344.          {
  345.             EndDialog( hDlg, TRUE ) ;
  346.             return ( TRUE ) ;
  347.          }
  348.          break;
  349.    }
  350.    return ( FALSE ) ;
  351.  
  352. } // end of AboutDlgProc()
  353.